package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuMapper;
import com.itheima.dao.PermissionMapper;
import com.itheima.dao.RoleMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Role> roles = roleMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),roles);
    }

    @Override
    public void add(Role role) {
        roleMapper.add(role);
        List<Integer> permissionIds = role.getPermissionIds();
        List<Integer> menuIds = role.getMenuIds();
        Integer roleId = role.getId();
        if (CollectionUtil.isNotEmpty(permissionIds)) {
            roleMapper.setRoleAndPermissionAssociation(roleId,permissionIds);
        }
        if (CollectionUtil.isNotEmpty(menuIds)) {
            roleMapper.setRoleAndMenuAssociation(roleId,menuIds);
        }
    }

    @Override
    public Map findById4Edit(Integer id) {
        Role role = roleMapper.findById(id);
        Map map = new HashMap();
        List<Permission> permissions = permissionMapper.findAll();
        List<Integer> permissionIds = roleMapper.findPermissionIds(id);
        List<Integer> menuIds = roleMapper.findMenuIds(id);
        map.put("role",role);
        map.put("permissions",permissions);
        map.put("permissionIds",permissionIds);
        map.put("menuIds",menuIds);
        return map;
    }

    @Override
    public void edit(Role role) {
        roleMapper.editRole(role);
        Integer roleId = role.getId();
        roleMapper.deleteRoleAndPermissionAssociation(roleId);
        roleMapper.deleteRoleAndMenuAssociation(roleId);
        List<Integer> permissionIds = role.getPermissionIds();
        List<Integer> menuIds = role.getMenuIds();
        if (CollectionUtil.isNotEmpty(permissionIds)) {
            roleMapper.setRoleAndPermissionAssociation(roleId,permissionIds);
        }
        if (CollectionUtil.isNotEmpty(menuIds)) {
            roleMapper.setRoleAndMenuAssociation(roleId,menuIds);
        }
    }

    @Override
    public void deleteById(Integer id) {
        Integer rpCount = roleMapper.getRoleAndPermissionCountByRoleId(id);
        Integer rmCount = roleMapper.getRoleAndMenuCountByRoleId(id);
        if (rpCount > 0 || rmCount > 0) {
            throw new RuntimeException("存在引用，不能删除");
        } else {
            roleMapper.deleteByPrimaryKey(id);
        }
    }
}
